import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import { getToken } from '@/utils/auth'
import { Notify } from 'vant'
Vue.use(Router)

const routes = [
  {
    path: '*',
    redirect: '/home'
  },
  // 登录
  {
    path: '/login',
    name: 'login',
    component: () => import('@/view/account/login'),
    meta: {
      title: '登录',
      full: true
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/view/account/register'),
    meta: {
      title: '注册',
      full: true
    }
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('@/view/user'),
    meta: {
      title: '圈友'
    }
  },
  {
    path: '/topic',
    name: 'topic',
    component: () => import('@/view/topic'),
    meta: {
      title: '话题'
    }
  },
  {
    path: '/topicInfo',
    name: 'topicInfo',
    component: () => import('@/view/topic/info'),
    meta: {
      title: '话题详情页'
    }
  },
  {
    path: '/post',
    name: 'post',
    component: () => import('@/view/post'),
    meta: {
      title: '帖子详情页'
    }
  },
  {
    path: '/listDtalis',
    name: 'listDtalis',
    component: () => import('@/view/llist_dtails'),
    meta: {
      title: '团购列表页'
    }
  },
  {
    path: '/release',
    name: 'release',
    component: () => import('@/view/release'),
    meta: {
      title: '我的发布'
    }
  },
  {
    path: '/group',
    name: 'group',
    component: () => import('@/view/group'),
    meta: {
      title: '团购项目'
    }
  },
  {
    path: '/signUp',
    name: 'signUp',
    component: () => import('@/view/signUp'),
    meta: {
      title: '团购报名页'
    }
  },
  {
    path: '/applyfor',
    name: 'applyfor',
    component: () => import('@/view/applyfor'),
    meta: {
      title: '房资深申认证请页'
    }
  },
  {
    path: '/attention',
    name: 'attention',
    component: () => import('@/view/attention'),
    meta: {
      title: '我关注的新房团购'
    }
  },
  {
    path: '/firends',
    name: 'firends',
    component: () => import('@/view/firends'),
    meta: {
      title: '我的好友'
    }
  },
  {
    path: '/firendInfo',
    name: 'firendInfo',
    component: () => import('@/view/firends/info'),
    meta: {
      title: '好友详情'
    }
  },
  {
    path: '/circle',
    name: 'circle',
    component: () => import('@/view/circle'),
    meta: {
      title: '我的圈子'
    }
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/view/home'),
    meta: {
      title: '首页',
      hasTabber: true
    }
  },
  {
    path: '/senior',
    name: 'senior',
    component: () => import('@/view/senior'),
    meta: {
      title: '房资深'
    }
  },
  {
    path: '/BBS',
    name: 'BBS',
    component: () => import('@/view/BBS'),
    meta: {
      title: '房市论坛',
      hasTabber: true
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('@/view/mine'),
    meta: {
      title: '我的',
      hasTabber: true
    }
  },
  {
    path: '/myTopic',
    name: 'myTopic',
    component: () => import('@/view/mine/myTopic'),
    meta: {
      title: '我关注的话题'
    }
  },
  {
    path: '/waitPost',
    name: 'waitPost',
    component: () => import('@/view/waitPost'),
    meta: {
      title: '发帖'
    }
  },
  {
    path: '/userDtails',
    name: 'userDtails',
    component: () => import('@/view/userDtails'),
    meta: {
      title: '圈友详情'
    }
  },
  {
    path: '/roomFriends',
    name: 'roomFriends',
    component: () => import('@/view/room_ friends'),
    meta: {
      title: '房友圈'
    }
  },
  {
    path: '/questionAnswer',
    name: 'questionAnswer',
    component: () => import('@/view/answer'),
    meta: {
      title: '房问答'
    }
  },
  {
    path: '/releaseAnswer',
    name: 'releaseAnswer',
    component: () => import('@/view/releaseAnswer'),
    meta: {
      title: '发布问答'
    }
  },
  {
    path: '/answerDatls',
    name: 'answerDatls',
    component: () => import('@/view/answer_details'),
    meta: {
      title: '问答详情'
    }
  }
]

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '')
})

const router = new Router({ routes })

const whiteList = ['/login','/register'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
    document.title = title
  }
  const hasToken = getToken()
  if (hasToken) {
    if (to.path === '/login') {
      next({path: '/'})
    } else {
      const hasInfo = store.getters.uid
      if (hasInfo) {
        next()
      } else {
        try {
          await store.dispatch('user/getInfo')
          next({ ...to, replace: true })
        } catch (error) {
          await store.dispatch('user/resetToken')
          Notify({
            message: error || 'Has Error',
            background: '#fa1919',
            duration: 1 * 1000
          })
          next('/login')
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})

export { router }
